// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cantor/unresolved/visitor.h>

namespace cantor::unresolved {
    class UnresolvedSelectStmtOpts;
    class UnresolvedSelectField;
    class UnresolvedTableRef;
    class UnresolvedWhereClause;
    class UnresolvedOrderByClause;
    class UnresolvedLimitClause;
    class UnresolvedGroupByClause;
    class UnresolvedHavingClause;
    class UnresolvedWithClause;

    class UnresolvedSelectStmt : public UnresolvedVisitor<UnresolvedSelectStmt> {
    public:
        static constexpr const cantor::pb::ast::StmtType TYPE = cantor::pb::ast::StmtType::STMT_SELECT;

        UnresolvedSelectStmt(const cantor::pb::ast::Statement &node) : instance(node.select_stmt()) {
        }

        UnresolvedSelectStmt(const cantor::pb::ast::SelectStmt &node) : instance(node) {
        }

        ~UnresolvedSelectStmt() override = default;

        const pb::Location &location_impl() const {
            return instance.location();
        }

        const pb::Status &error_status_impl() const {
            return instance.status();
        }

        bool has_select_opt() const {
            return instance.has_select_opt();
        }

        UnresolvedSelectStmtOpts select_opt() const;

        int32_t field_count() const {
            return instance.fields_size();
        }

        UnresolvedSelectField field(int32_t index) const;

        bool has_table_ref() const {
            return instance.has_table_refs();
        }

        UnresolvedTableRef table_ref() const;

        bool has_where_clause() const {
            return instance.has_where();
        }

        UnresolvedWhereClause where() const;

        bool has_group_clause() const {
            return instance.has_group();
        }

        UnresolvedGroupByClause group() const;

        bool has_having() const {
            return instance.has_having();
        }

        UnresolvedHavingClause having() const;

        bool has_order_clause() const {
            return instance.has_order();
        }

        UnresolvedOrderByClause order() const;

        bool has_limit() const {
            return instance.has_limit();
        }

        UnresolvedLimitClause limit() const;

        bool has_with_clause() const {
            return instance.has_with();
        }

        UnresolvedWithClause with() const;

        bool is_in_braces(bool v = false) {
            if (instance.has_is_in_braces()) {
                return instance.is_in_braces();
            }
            return v;
        }

        cantor::pb::ast::SelectLock lock(cantor::pb::ast::SelectLock lk = cantor::pb::ast::SelectLock::SL_NONE) const {
            if (instance.has_lock()) {
                return instance.lock();
            }
            return lk;
        }

        const std::string &option() const {
            return instance.option();
        }

        const cantor::pb::ast::SelectStmt &instance;
    };
} // namespace cantor::unresolved
